package com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.viewholder;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DetailInterfaceActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.RoundImageView;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/16
 * 功能描述：
 * 联系方式：1037438704@qq.com
 */

public class HomeAdViewHolder extends BaseViewHolder<Map<String, String>> {
    @BindView(R.id.text_on_line)
    TextView text_on_line;
    @BindView(R.id.image_head)
    RoundImageView imageHead;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_sex_age)
    TextView text_sex_age;
    @BindView(R.id.image_collection)
    ImageView imageCollection;
    @BindView(R.id.ll_whole_onclick)
    LinearLayout llWholeOnclick;
    @BindView(R.id.text_identification)
    TextView textIdentification;
    @BindView(R.id.text_city)
    TextView textCity;
    public int jump = 0;

    public void setJump(int jump) {
        this.jump = jump;
    }

    public HomeAdViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(final Map<String, String> list) {
        String mySex = Preferences.getInstance().getString(getContext(), "user", "sex");
        textTitle.setText(list.get("nickname"));
        GlideUtils.glideFille(getContext(), list.get("head_pic"), imageHead);
        textCity.setText("\t|\t" + list.get("region_name"));
        // 是否收藏 1是2否
        if (list.get("is_like").equals("1")) {
            GlideUtils.glideFille(getContext(), R.mipmap.ic_detail_collection_true, imageCollection);
        } else {
            GlideUtils.glideFille(getContext(), R.mipmap.ic_detail_collection, imageCollection);
        }
        text_on_line.setText(list.get("nickname"));

        text_on_line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        // distance 距离     // online  是否在线
        text_on_line.setText(list.get("distance") + "·" + list.get("online"));
        String sex = list.get("sex");
        Drawable drawable = null;
        if (sex.equals("2")) {
            drawable = getContext().getResources().getDrawable(R.mipmap.ic_girl_sex);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            String identification = list.get("identification");
            if (identification.equals("1")) {
                textIdentification.setText("未认证");
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("已认证");
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }
        } else if (sex.equals("1")) {
            drawable = getContext().getResources().getDrawable(R.mipmap.icon_boy_my);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            String vip = list.get("vip");
            // 是否vip 1否 2是
            if (vip != null) {
                if (vip.equals("1")) {
                    textIdentification.setText("非会员");
                    textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                    textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
                } else {
                    textIdentification.setText("会员");
                    textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                    textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
                }
            }
        }
        text_sex_age.setCompoundDrawables(drawable, null, null, null);
        text_sex_age.setText(list.get("age"));
        String is_like = list.get("is_like");
        if (is_like.equals("1")) {
            imageCollection.setImageResource(R.mipmap.ic_detail_collection_true);
        } else {
            imageCollection.setImageResource(R.mipmap.ic_detail_collection);
        }
        //在这里写点击事件
        llWholeOnclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mySex.equals(sex)) {
                    ToastUtil.show("不能查看同性別的人");
                    return;
                }
                mOnClick.onClick(getAdapterPosition(), list, 1);
            }
        });
        String token = Preferences.getInstance().getString(getContext(), "user", "token");

        imageCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mySex.equals(sex)) {
                    ToastUtil.show("不能收藏同性别的人");
                    return;
                }
                HttpRequest.POST(getContext(), "Mylike/addMylike"
                        , new Parameter()
                                .add("token", token)
                                .add("cover_member_id", list.get("id"))
                        , new ResponseListener() {
                            @Override
                            public void onResponse(String response, Exception error) {
                                if (error == null) {
                                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                    if (map.get("code").equals("1")) {
                                        ToastUtil.show(map.get("message"));
                                        // 是否收藏 1是 2否
                                        if (map.get("message").equals("收藏成功")) {
                                            imageCollection.setImageResource(R.mipmap.ic_detail_collection_true);
                                        } else {
                                            imageCollection.setImageResource(R.mipmap.ic_detail_collection);
                                        }
                                    }
                                }
                            }
                        });
            }
        });
    }

    private ItemOnClick mOnClick = null;

    //点击事件的转嫁接口
    public interface ItemOnClick {
        void onClick(int position, Map<String, String> data, int type);
    }

    public void setmOnClick(ItemOnClick mOnClick) {
        this.mOnClick = mOnClick;
    }
}